/**
 * Created by zhangpeng on 2015/6/3.
 */
var evernote = require('evernote').Evernote;

var SimpleEvernote = function (option) {
    if (!(this instanceof SimpleEvernote)) {
        return new SimpleEvernote(option);
    }
    this.token = option.token;
    var client = new evernote.Client(option);
    this.noteStore = client.getNoteStore();
}
//查询evernote笔记本
SimpleEvernote.prototype.findNotes = function (words, order,noteBook, callback) {

    var noteFilter = new evernote.NoteFilter;
    noteFilter.words = words;
    noteFilter.order = evernote.NoteSortOrder[(order)];// 'UPDATED' or 'CREATED'
    noteFilter.ascending = false;
    noteFilter.inactive = false;
    noteFilter.notebookGuid = noteBook;

    var offset = 0;
    var count = 10;
    var spec = new evernote.NotesMetadataResultSpec;

    this.noteStore.findNotes(this.token, noteFilter, offset, count, spec, function (err, notes_list) {

        if (err) throw err;
        callback(notes_list);

    });

}
//根据guid查询evernote笔记
SimpleEvernote.prototype.getNote = function (guid, callback) {
    this.noteStore.getNote(this.token, guid, true, true, false, false,
        function (err, note) {

            if (err) throw err;

            callback(note);


        });
}

SimpleEvernote.prototype.getNotebookByName = function(name,callback){
    this.noteStore.listNotebooks(function (err, notebooks) {
     if (err) throw err;
      for(i = 0;i < notebooks.length;i++){
        if (notebooks[i].name == name)  {
            callback(notebooks[i]);
        }
      }


});
}

module.exports = SimpleEvernote;